package com.ling.common.library.base.mvc

import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author Ling
 * @date :2024/8/23 11:22
 */
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity(){

    protected val mBinding get() = _binding!!
    private var _binding :VB ?= null
    //activity回调
    private var _onActivityResult : ((result : ActivityResult?)-> Unit)?=null
    //activity启动容器
    private var _launcher: ActivityResultLauncher<Intent>?= null


    protected abstract fun bindLayout():VB
    protected abstract fun initPage()

    //是否开启eventbus
    open fun enableEventbus() = false
    //是否需要开启activity回调
    open fun registerActivityResult() = false
    //页面是否选需要监听返回事件
    open fun handleBackPressed() = false
    //页面回调返回事件
    open fun onBack(){}

    //EventBus消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventbus(msg: String) {}


    //初始化启动容器
    private fun initStartLauncher() {
        _launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            _onActivityResult?.let {
                it(result)
            }
        }
    }

    //注册拦截返回事件
    private fun initBackPressed(){
        onBackPressedDispatcher.addCallback(this,object : OnBackPressedCallback(handleBackPressed()){
            override fun handleOnBackPressed() {
                onBack()
            }
        })
    }

    //启动一个需要回调的Activity
    open fun startActivityForResult(aClass: Class<*>?, bundle: Bundle ?= null, onActivityResult : (result : ActivityResult?)-> Unit) {
        if (_launcher == null) {
            throw RuntimeException("Please override initStartLauncher() and return true,before using startActivityForResult()")
        } else {
            this._onActivityResult = onActivityResult
            val intent = Intent(this, aClass)
            if(bundle != null){
                intent.putExtras(bundle)
            }
            _launcher?.launch(intent)
        }
    }

    //启动一个需要回调的Activity
    open fun startActivityForResult(intent: Intent, onActivityResult : (result : ActivityResult?)-> Unit) {
        if (_launcher == null) {
            throw RuntimeException("Please override initStartLauncher() and return true,before using startActivityForResult()")
        } else {
            this._onActivityResult = onActivityResult
            _launcher?.launch(intent)
        }
    }

    //启动一个Activity
    open fun startActivity(aClass: Class<*>?, bundle: Bundle ?= null) {
        val intent = Intent(this, aClass)
        if(bundle != null){
            intent.putExtras(bundle)
        }
        startActivity(intent)
    }





    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = bindLayout()
        setContentView(_binding?.root)
        if(handleBackPressed()){
            initBackPressed()
        }
        if(registerActivityResult()){
            initStartLauncher()
        }
        initPage()
        if(enableEventbus()){
            EventBus.getDefault().register(this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(enableEventbus()){
            EventBus.getDefault().unregister(this)
        }
        this._onActivityResult = null
        this._launcher = null
        this._binding = null
    }
}